package cn.ctodb.mall.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.data.elasticsearch.annotations.Document;

import javax.persistence.*;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import java.util.Objects;

/**
 * A BackOrder.
 */
@Entity
@Table(name = "back_order")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "backorder")
public class BackOrder implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "back_sn")
    private String backSn;

    @OneToMany(mappedBy = "backOrder")
    @JsonIgnore
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<BackGoods> goods = new HashSet<>();

    @ManyToOne
    private Member member;

    @ManyToOne
    private Shop shop;

    @ManyToOne
    private DeliveryOrder deliveryOrder;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getBackSn() {
        return backSn;
    }

    public BackOrder backSn(String backSn) {
        this.backSn = backSn;
        return this;
    }

    public void setBackSn(String backSn) {
        this.backSn = backSn;
    }

    public Set<BackGoods> getGoods() {
        return goods;
    }

    public BackOrder goods(Set<BackGoods> backGoods) {
        this.goods = backGoods;
        return this;
    }

    public BackOrder addGoods(BackGoods backGoods) {
        goods.add(backGoods);
        backGoods.setBackOrder(this);
        return this;
    }

    public BackOrder removeGoods(BackGoods backGoods) {
        goods.remove(backGoods);
        backGoods.setBackOrder(null);
        return this;
    }

    public void setGoods(Set<BackGoods> backGoods) {
        this.goods = backGoods;
    }

    public Member getMember() {
        return member;
    }

    public BackOrder member(Member member) {
        this.member = member;
        return this;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    public Shop getShop() {
        return shop;
    }

    public BackOrder shop(Shop shop) {
        this.shop = shop;
        return this;
    }

    public void setShop(Shop shop) {
        this.shop = shop;
    }

    public DeliveryOrder getDeliveryOrder() {
        return deliveryOrder;
    }

    public BackOrder deliveryOrder(DeliveryOrder deliveryOrder) {
        this.deliveryOrder = deliveryOrder;
        return this;
    }

    public void setDeliveryOrder(DeliveryOrder deliveryOrder) {
        this.deliveryOrder = deliveryOrder;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        BackOrder backOrder = (BackOrder) o;
        if (backOrder.id == null || id == null) {
            return false;
        }
        return Objects.equals(id, backOrder.id);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(id);
    }

    @Override
    public String toString() {
        return "BackOrder{" +
            "id=" + id +
            ", backSn='" + backSn + "'" +
            '}';
    }
}
